<?php

namespace app\admin\controller\Product;

use app\common\controller\Backend;

class Type extends Backend
{

    protected $model = null;
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = '';

    public function _initialize(){
        parent::_initialize();
        $this->model = model('Product.Type');
    }

    // 查看
    public function index(){
        //设置过滤方法  过滤空格和标签信息
        $this->request->filter(['trim', 'strip_tags']);

        if ($this->request->isAjax()) {
            //主键
            if ($this->request->request("keyField")) {
                // 自定义搜索查询
                return $this->selectpage();
            }

            // 生成查询所需要的条件,排序方式
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 添加
    public function add(){
        if ($this->request->isPost()) {
            $params = $this->request->param("row/a");
            if($params){
                $validate = validate('common/Product/Type');
                if (!$validate->check($params)) {
                    return $this->error($validate->getError());
                    exit;
                }

                $result = $this->model->save($params);
                if ($result === false) {
                    $this->error($this->model->getError());
                    exit;
                }

                $this->success('添加成功');
                exit;
            }
        }

        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null){
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('分类不存在'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->param("row/a");

            if ($params) {
                $params['id'] = $ids;
                $result = $this->model->isUpdate(true)->save($params);
                if ($result === false) {
                    $this->error($this->model->getError());
                    exit;
                }

                $this->success('编辑成功');
                exit;
            }
        }

        $this->assign([
            'row' => $row
        ]);
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null){
        $list = $this->model->select();
        if (!$list) {
            $this->error('分类不存在');
        }

        $result = $this->model->destroy($ids);
        if ($result === false) {
            $this->error($this->model->getError());
            exit;
        }else{
          $this->success('删除成功');
          exit;
        }
    }

}
